// 根地址
export const baseUrl = ''

// 白名单，不需要携带token就允许被访问的接口
const whiteApiList = ['/api/login/mobile', '/api/login', '/api/register/verify', '/api/register/reset']
export default async function(api, args, withLoading) {
	try {
		//前端遮挡
		if (withLoading) {
			uni.showLoading({});
		}
		// 设置前端数据
		let token = uni.getStorageSync('uni_id_token');
		//是否登陆
		// if (!token && !whiteApiList.includes(api)) {
		// 	uni.redirectTo({
		// 		url: '/pages/login/index'
		// 	});
		// 	if (withLoading) {
		// 		uni.hideLoading();
		// 	}
		// }
		//用户已登录,获取用户数据
		let res = await new Promise((
			resolve,
			reject
		) => {
			// api = baseUrl + api
			uni.request({
				url: 'https://fc-mp-d8213eba-e1a7-4039-8d88-7333a54d5e73.next.bspapp.com/fun',
				data: {
					api,
					args,
					token,
				},
				method: 'POST',
				success: (res) => {
					resolve(res.data);
				},
				fail: (res) => {
					reject(res.errMsg);
				}
			})
		});

		//前端遮挡
		if (withLoading) {
			uni.hideLoading();
		}
		//报错
		if (res.success === false) {
			uni.showModal({
				title: "操作失误",
				content: res.errorMessage,
			});
			return false;
		} else {
			return res;
		}
	} catch (err) {
		if (withLoading) {
			uni.hideLoading
		}
		uni.showModal({
			title: "网络错误",
			content: err.message,
		});
		console.log('err:' + err)
		return false;
	}
}